package com.feicheng.controller.wx;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.feicheng.common.exception.SystemException;
import com.feicheng.common.result.ResultInfo;
import com.feicheng.common.util.StringGenerator;
import com.feicheng.persistence.entity.Adz;
import com.feicheng.persistence.entity.Afj;
import com.feicheng.persistence.entity.Amd;
import com.feicheng.persistence.entity.Amj;
import com.feicheng.persistence.mapper.AmdMapper;
import com.feicheng.persistence.service.IAdzService;
import com.feicheng.persistence.service.IAfjService;
import com.feicheng.persistence.service.IAmdService;
import com.feicheng.persistence.service.IAmjService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController("/wx/md")
@RequestMapping("/wx/md")
public class MdController {

    @Autowired
    IAmdService iAmdService;

    @Autowired
    IAdzService iAdzService;

    @Autowired
    IAfjService iAfjService;

    @Autowired
    IAmjService iAmjService;

    @Autowired
    AmdMapper amdMapper;

    @PostMapping("/list/get")
    public ResultInfo getMdByList(@RequestBody JSONObject param) {
        QueryWrapper<Amd> qw = new QueryWrapper<>();
        String mdmc = param.getString("mdmc");
        if (StringUtils.hasLength(mdmc)) {
            qw.like("mdmc", mdmc);
        }
        param.put("mds", iAmdService.list(qw));
        return ResultInfo.success(param);
    }

    /**
     * 门店分页查询
     * @param param
     * @return
     */
    @PostMapping("/page/get")
    public ResultInfo getMdByPage(@RequestBody JSONObject param) {

        Integer current = param.getInteger("current");
        Integer size = param.getInteger("size");

        if (current == null || current < 0) {
            current = 1;
        }

        if (size == null || size < 0) {
            size = 16;
        }

        // 构建分页
        Page<JSONObject> page = new Page<>(current, size);
        QueryWrapper<Amd> qwmd = new QueryWrapper<>();

        String mdmc = param.getString("mdmc");
        if (StringUtils.hasLength(mdmc)) {
            qwmd.like("mdmc",mdmc);
        }

        String dzid = param.getString("dzid");
        if (StringUtils.hasLength(dzid)) {
            qwmd.eq("dzid",dzid);
        }

        Boolean flg = param.getBoolean("flg");
        if (null != flg) {
            qwmd.eq("flg",flg);
        }

        String jd = param.getString("jd");
        String wd = param.getString("wd");
        page = amdMapper.getStoreWithDzByPage(page, qwmd, jd, wd);
        param.put("mds", page);
        return ResultInfo.success(param);
    }

    /**
     * 门店详细查询
     * @param param
     * @return
     */
    @PostMapping("/info/get")
    public ResultInfo getMdInfo(@RequestBody JSONObject param) {

        String mdid = param.getString("mdid");
        if (!StringUtils.hasLength(mdid)) {
            throw new SystemException("id必须指定");
        }

        Amd md = iAmdService.getById(mdid);
        param.put("md", md);

        QueryWrapper<Afj> fjqw = new QueryWrapper<>();
        fjqw.eq("mdid", mdid);
        param.put("fjs", iAfjService.list(fjqw));

        Adz adz = iAdzService.getById(md.getDzid());
        param.put("adz", adz);

        return ResultInfo.success(param);
    }

    @PostMapping("/info/put")
    public ResultInfo updateMdInfo(@RequestBody JSONObject param) {

        Amd md = param.getObject("md", Amd.class);
        List<Afj> fjs = param.getObject("fjs", new TypeReference<List<Afj>>() {});
        List<Amj> mjs = param.getObject("mjs", new TypeReference<List<Amj>>() {});

        iAmdService.updateById(md);
        param.put("md", md);

        QueryWrapper<Afj> fjqw = new QueryWrapper<>();
        fjqw.eq("mdid", md.getMdid());
        iAfjService.remove(fjqw);

        QueryWrapper<Amj> mjqw = new QueryWrapper<>();
        mjqw.eq("mdid", md.getMdid());
        iAmjService.remove(mjqw);

        if (null != fjs && fjs.size() > 0) {
            fjs.forEach(fj -> {
                if (!StringUtils.hasLength(fj.getFjid())) {
                    fj.setFjid(StringGenerator.getID("FJ"));
                }
                fj.setMdid(md.getMdid());
            });
            iAfjService.saveBatch(fjs);
            param.put("fjs", fjs);
        }

        if (null != mjs) {
            mjs.forEach(mj -> {
                Amj mjitem = iAmjService.getById(mj.getSn());
                if (null != mjitem) {
                    throw new SystemException("您所输入的SN【"+mj.getSn()+"】已经被占用，请确认设备SN并重新输入。");
                }
                if (!StringUtils.hasLength(mj.getMjid())) {
                    mj.setMjid(mj.getSn());
                }
                mj.setMdid(md.getMdid());
            });
            iAmjService.saveBatch(mjs);
            param.put("mjs", mjs);
        }

        return ResultInfo.success(param);
    }

    @PostMapping("/open")
    public ResultInfo open(@RequestBody JSONObject param) {
        String mdid = param.getString("mdid");
        Amd amd = new Amd();
        amd.setMdid(mdid);
        amd.setFlg(true);
        iAmdService.updateById(amd);
        return ResultInfo.success();
    }

    @PostMapping("/close")
    public ResultInfo close(@RequestBody JSONObject param) {
        String mdid = param.getString("mdid");
        Amd amd = new Amd();
        amd.setMdid(mdid);
        amd.setFlg(false);
        iAmdService.updateById(amd);
        return ResultInfo.success();
    }

}
